#### PACKAGES ####

library(haven)
library(dplyr)
library(ggplot2)
library(tidyr)
library(coin)
library(RVAideMemoire)
library(broom)
library(kableExtra)


sink("../output/experiment21.log")

#### DATA #### 
dat21 <- read_sav("../input/CCES21_Common_OUTPUT_timing.sav")

#### DATATPREP ####

# indicators for inattentivness

dat21$inconsistenta <- 0
dat21$inconsistenta[dat21$CC21_323a==1 & dat21$CC21_323e==1] <- 1

dat21$inconsistent <- 0
dat21$inconsistent[dat21$CC21_321c==1 & dat21$CC21_321d==1] <- 1

dat21$gotprompt <- NA
dat21$gotprompt[dat21$CC21_321c==1 & dat21$CC21_321d==1 & is.na(dat21$CC21_321x)] <- 0
dat21$gotprompt[!is.na(dat21$CC21_321x)] <- 1

dat21$condition <- "Did not contradict"
dat21$condition[dat21$CC21_321c==1 & dat21$CC21_321d==1 & is.na(dat21$CC21_321x)] <- "No prompt"
dat21$condition[!is.na(dat21$CC21_321x)] <- "Prompt"

dat21$gunsstraight <- 0
dat21$abortstraight <- 0
dat21$envstraight <- 0

#### Summary Stats #### 
dat21 %>% group_by(gotprompt) %>%
  dplyr::summarise(contradict = mean(inconsistenta, na.rm=T),
                  gunstime = median(page_CC21_322grid_timing, na.rm=T),
                   aborttime = median(page_CC21_323grid_timing, na.rm=T),
                   envtime = median(page_CC21_324grid_timing, na.rm=T))

dat21 %>% group_by(gotprompt, CC21_321x) %>%
  dplyr::summarise(contradict = mean(inconsistenta, na.rm=T),
                   gunstime = median(page_CC21_322grid_timing, na.rm=T),
                   aborttime = median(page_CC21_323grid_timing, na.rm=T),
                   envtime = median(page_CC21_324grid_timing, na.rm=T))

#### Figure 2: Graph page timings before and after experiment ####
pdf("../output/pagetimings_experiment.pdf", width=7, height=5)
dat21 %>% group_by(condition) %>%
  dplyr::summarise(page1 = median(page_approve_timing, na.rm=T),
                   page2 = median(page_CC21_320grid_timing, na.rm=T),
                   page3 = NA,
                   page4 = median(page_CC21_322grid_timing, na.rm=T),
                   page5 = median(page_CC21_323grid_timing, na.rm=T),
                   page6 = median(page_CC21_324grid_timing, na.rm=T),
                   page7 = median(page_CC21_325grid_timing, na.rm=T),
                   page8 = median(page_CC21_330grid_timing, na.rm=T),
                   page9 = median(page_CC21_350_timing, na.rm=T)) %>%
  pivot_longer(cols=page1:page9) %>%
  ggplot(aes(x=name, y=value, shape=as.factor(condition), color=as.factor(condition), group=as.factor(condition))) + 
  geom_line() + ylab("Median page timing") + xlab("Page relative to experiment") +
  scale_color_brewer(palette="Dark2") + ylim(0,90) +
  geom_point(alpha=.6) + 
  theme_minimal() +
  theme(plot.background = element_blank(),
        panel.grid.minor = element_blank(),
        legend.title = element_blank()) +
  geom_vline(xintercept=2.1, linetype=5) + geom_vline(xintercept=3.9, linetype=5) + 
  annotate("text", x=3, y=30, label= "Experiment", angle=90) +
  scale_x_discrete(labels=c(-2, -1, 0, 1, 2, 3, 4, 5, 6))
dev.off()
print("Figure 2 Done")

#### KS test #### 

dat21$times <- dat21$page_CC21_322grid_timing + dat21$page_CC21_323grid_timing + dat21$page_CC21_324grid_timing + dat21$page_CC21_325grid_timing + dat21$page_CC21_330grid_timing 
dat21$times[dat21$times>361.3534] <- 361.3534
print("KS Test:")
ks.test(times ~ condition, data=dat21, subset = condition %in% c("No prompt", "Prompt"))


#### Figure 3: Differences in abortion inconsistencies ##### 

pdf("../output/experiment_abortion_contradict.pdf", width=4, height=4)
dat21 %>% group_by(condition) %>%
  dplyr::summarise_each(funs(mean, sd, se=sd(.)/sqrt(n())), inconsistenta) %>%
  ggplot() + 
  geom_bar(aes(x=condition, y=mean), stat="identity", fill="navy", alpha=.7, width=.5) +
  geom_errorbar(aes(x=condition, ymin=mean-se*1.96, ymax=mean+se*1.96), width=.1) +
  ylab("Contradiction rate on Abortion grid") + xlab("Group") +
  theme_minimal()
dev.off()
print("Figure 3 Done")

#### Appendix B6: median test ##
questions <- c("page_approve_timing", "page_CC21_320grid_timing", "page_CC21_322grid_timing", "page_CC21_323grid_timing", "page_CC21_324grid_timing", "page_CC21_325grid_timing", "page_CC21_330grid_timing", "page_CC21_350_timing")

question_count <- -3
dat21_exp <- dat21 %>% subset(condition != "Did not contradict")
for (var in questions) {
  formula_str <- paste0(var, "~ condition")
  t_test_result <- tidy(mood.medtest(as.formula(formula_str), data = dat21_exp, exact = FALSE))
  if (var == "page_CC21_322grid_timing") {
    question_count <- question_count + 2
  } else {
    question_count <- question_count + 1
  }
  t_test_result$Count <- question_count
  if (var == "page_approve_timing") {
    combined_df <- t_test_result %>% dplyr::select(Count, p.value)
  } else {
    combined_df <- t_test_result %>% dplyr::select(Count, p.value) %>% rbind(combined_df)
  }
}

combined_df <- combined_df %>% arrange(Count) %>% rename("Page relative to experiment" = "Count","P-value of median difference" = "p.value")
binary_output <- with(combined_df, kable(combined_df, "latex", linesep = "", digits=3, booktabs = TRUE))
writeLines(binary_output, "../output/experiment_timings.tex")
print("Appendix Table B6 Done")
  

